<?php

/**
 * uniBoard ACP New Group
 *
 * Light and fast forum solution for web 2.0-like communities.
 *
 * @package		uniBoard
 * @author		Rafał Pitoń
 * @license		http://www.gnu.org/licenses/gpl-3.0.html
 * @since		Version 0.1
 * 
 * ------------------------------------------------------------------------
 * 
 * $Date: 2010-09-15 17:35:38 +0000 (Wed, 15 Sep 2010) $
 * $Revision: 61 $
 * $Author: rafio.xudb@gmail.com $
 */

// ------------------------------------------------------------------------

class action_new_group{
	
	/**
	 * Main class pointer
	 *
	 * @var object
	 */
	
	private $appCore		= NULL;
	
	/**
	 * Our api
	 *
	 * @var object
	 */
	
	private $api			= NULL;
	
	/**
	 * Avaiable Ranks
	 *
	 * @var array
	 */
	
	private $ranks			= array();
	
	/**
	 * Avaiable Masks
	 *
	 * @var array
	 */
	
	private $masks			= array();
	
	// ------------------------------------------------------------------------

	/**
	 * Initialise Action
	 *
	 * @param object $appCore
	 */
	
	function __construct( $appCore){
		
		// Store appCore pointer
		$this -> appCore = $appCore;
		
		// Create API
		$this -> api = new api_user_group( $appCore);
		
		// Get Masks
		$this -> masks = $appCore -> getMasks();
			
		// Add empty Rank
		$this -> ranks[0] = $appCore -> lang['rank_use_none'];
		
		// Ranks
		$appCore -> db -> query( 'SELECT rank_id, rank_name FROM ' . DB_PREFIX . 'user_ranks WHERE rank_special = \'1\'');
		
		// Pass them to array
		while ( $rank_result = $appCore -> db -> fetch_array())
		{
			// Pass to array
			$this -> ranks[$rank_result[0]] = ($appCore -> conf['board_int_ranks'] ? $appCore -> ifString( htmlspecialchars( $rank_result[1])) : htmlspecialchars( $rank_result[1]));
		}
		
		// Flush Result
		$appCore -> db -> freeResult();
		
		// Update?
		if ( $appCore -> _POST( 'update') == 1)
		{
			// Fill Api
			$this -> api -> setGroupName( $appCore -> _POST( 'group_name'));
			$this -> api -> setGroupPrefix( $appCore -> _POST( 'group_prefix'));
			$this -> api -> setGroupSuffix( $appCore -> _POST( 'group_suffix'));
			$this -> api -> setGroupRank( $appCore -> _POST( 'group_rank'));
			$this -> api -> setGroupHidden( $appCore -> _POST( 'group_hidden'));
			$this -> api -> setGroupMasks( $appCore -> listInput( 'group_masks', false, array_keys( $this -> masks)));
			
			// Api All-rite?
			if ( $this -> api -> error)
			{
				// Draw Errors
				$appCore -> output -> error( join( '<br />', $this -> api -> errors), $appCore -> lang['new_group_form']);
				
				// Repeat Form
				$this -> drawForm();
			}
			else
			{
				// Save Api
				$this -> api -> saveData();
				
				// Set Log
				$appCore -> logAdmin( 'log_admin_created_new_user_group', htmlspecialchars( $appCore -> _POST( 'group_name')));
				
				// Redirect us back to list
				$appCore -> output -> drawRedirect( $appCore -> adminLink( 'act=groups'), $appCore -> lang['acp_group_saved']);
			}
		}
		else
		{
			// Draw Form
			$this -> drawForm();
		}
		
	}
	
	// ------------------------------------------------------------------------

	/**
	 * Draws Form
	 *
	 */
	
	function drawForm(){
	
		// Load fields templates
		$this -> appCore -> templates['form_inputs'] = new tpl_form_inputs( $this -> appCore);
				
		// Cache output
		ob_start();
				
		// Open Form
		echo $this -> appCore -> output -> openForm( $this -> appCore -> adminLink( 'act=new_group'));
		echo $this -> appCore -> drawHidden( 'update', 1);
		echo $this -> appCore -> output -> openFormBlock( $this -> appCore -> lang['acp_group_form_sect_name']);
		
		// Draw fields
		echo $this -> appCore -> output -> formItem( $this -> appCore -> lang['acp_group_form_name'], $this-> appCore -> drawTextInput( 'group_name', $this -> api -> group_name));
		echo $this -> appCore -> output -> formItem( $this -> appCore -> lang['acp_group_form_prefix'], $this-> appCore -> drawTextInput( 'group_prefix', $this -> api -> group_prefix), $this -> appCore -> lang['acp_group_form_affix_expl']);
		echo $this -> appCore -> output -> formItem( $this -> appCore -> lang['acp_group_form_suffix'], $this-> appCore -> drawTextInput( 'group_suffix', $this -> api -> group_suffix), $this -> appCore -> lang['acp_group_form_affix_expl']);
		echo $this -> appCore -> output -> formItem( $this -> appCore -> lang['acp_group_form_rank'], $this-> appCore -> drawList( 'group_rank', $this -> ranks, $this -> api -> group_rank));
		
		// New block
		echo $this -> appCore -> output -> closeFormBlock();
		echo $this -> appCore -> output -> openFormBlock( $this -> appCore -> lang['acp_group_form_sect_display']);
		
		// Draw Yes No Field
		echo $this -> appCore -> output -> formItem( $this -> appCore -> lang['acp_group_form_hidden'], $this-> appCore -> drawYesNo( 'group_hidden', $this -> api -> group_hidden));
		
		// New block
		echo $this -> appCore -> output -> closeFormBlock();
		echo $this -> appCore -> output -> openFormBlock( $this -> appCore -> lang['acp_group_form_sect_masks']);
		
		// Draw Perms Multilist
		echo $this -> appCore -> output -> formItem( $this -> appCore -> lang['acp_group_form_masks'], $this-> appCore -> drawMultiList( 'group_masks', $this -> masks, $this -> api -> group_masks));
				
		// Close Form
		echo $this -> appCore -> output -> submitButton( $this -> appCore -> lang['new_group_form_submit']);
		echo $this -> appCore -> output -> closeFormBlock();
		echo $this -> appCore -> output -> closeForm();
		
		// Draw output
		$this -> appCore -> output -> draw( $this -> appCore -> lang['new_group_form'], array(
			$this -> appCore -> adminLink( 'act=groups') => $this -> appCore -> lang['acp_user_groups'],
			'' => $this -> appCore -> lang['new_group_form']
		), ob_get_clean());
			
	}

}